<?php
	require("frmCookie.php");
	
	$iOpcion = $_GET[iOpcion];
	$iMensaje = $_GET[iMensaje];
	
	switch ($iMensaje)
	{
		case 1:
		{
			$mensaje = "El movimiento se registro con exito.";
			break;
		}
	}
	
	switch($iOpcion)
	{
		case 3:
		{
			$mySQL = "SELECT * FROM equipostransito WHERE nroRemito='" . $_POST['txtRemitoDef'] ."'";
			$result = readSQL($mySQL);
			foreach($result as $value)
			{
				$mySQL = "UPDATE ordenes SET idAlmacen=" . $value['idAlmacen'] . " WHERE idOrden=" . $value['idOrden'];
				executeSQL($mySQL);
			}
			$mySQL = "DELETE FROM equipostransito WHERE nroRemito='" . $_POST['txtRemitoDef'] . "'";
			executeSQL($mySQL);
			
            header("Location: movimientosEquipos.php?iOpcion=1&iMensaje=1");
			break;
		}
	}
?>
<html>
	<head>
    	<?php
		require("frmHeader.php");
		?>
		<script language="javascript" src="scripts/ajax.js"></script>
		<script language="javascript" src="scripts/validar.js"></script>
		<script language="javascript">
			
			function showRemito(str)
			{
				xmlHttp = GetXmlHttpObject();
				if (xmlHttp == null)
	  			{
	  				alert ("Your browser does not support AJAX!");
	  				return;
	  			}
	  			 
				var url = "ajax.php";
				url = url + "?id=" + str;
				url = url + "&op=42";
				url = url + "&q=" + frm.txtNroRemito.value;
				url = url + "&sid=" + Math.random();
				xmlHttp.onreadystatechange = stateChangedRemito;
				xmlHttp.open("GET",url,true);
				xmlHttp.send(null);
			}
	
			function stateChangedRemito() 
			{ 
				if (xmlHttp.readyState == 4)
				{ 
					if(xmlHttp.responseText != "")
					{
						document.frm.submit();
					}
					else
					{
						document.getElementById("txtHint").innerHTML = "Remito Inexistente.";
					}
				}
			}
			
			function valida(inOp)
			{
				switch(inOp)
				{
					case 1:
					{
						if(frm.txtNroRemito.value == "")
						{
							alert("Por favor ingrese el nro de remito.");
							frm.txtNroRemito.focus();
							return false;
						}
						
						showRemito('<?php echo $_SESSION['idSucursal']; ?>');
						
						return false;
						
						break;
					}
					case 2:
					{
						document.frm.submit();
						
						break;
					}
				}
			}
		</script>
	</head>
	<body>
    	<table cellpadding="0" cellspacing="0" align="center" class="tablaPrincipal">
        	<tr>
            	<td align="center">
                	<?php
					require("frmSession.php"); 
					
					//Redirecciona a SINPERMISOS.PHP en caso de no tener permisos
					switch ($iOpcion)
					{
						case 1:
						case 2:
						{
							
							if (!(in_array(45001, $_SESSION[permisos])))
							{
							?>
							  <script language="JavaScript" type="text/javascript">
							  <!--
							  window.location.href = "sinPermisos.php"
							  //-->
							  </script>
							<?php
							}
							$titulo = "Recepcion de Stock";
							break;
						}
						case 3:
						case 4:
						{
							if (!in_array(45001, $_SESSION[permisos]))
							{
							?>
							  <script language="JavaScript" type="text/javascript">
							  <!--
							  window.location.href = "sinPermisos.php"
							  //-->
							  </script>
							<?php
							}
							$titulo = "Recepcion de Stock";
							break;
						}
						case 5:
						case 6:
						case 7:
						{
							if (!in_array(45001, $_SESSION[permisos]))
							{
							?>
							  <script language="JavaScript" type="text/javascript">
							  <!--
							  window.location.href = "sinPermisos.php"
							  //-->
							  </script>
							<?php
							}
							$titulo = "Recepcion de Stock";
							break;
						}
					}
					
					?>
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<?php
					require("frmTitulo.php");
					?>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
            	<td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="menuPrincipal">
                    	<tr>
                        	<td>
								<?php
                                require("frmMenu.php");
                                ?>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="tituloPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $titulo; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="estadoPantalla">
                    	<tr>
                        	<td align="center">
                            	<p class="texto" align="left"><?php echo $mensaje; ?></p>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
             <tr>
            	<td height="20">&nbsp;
                	
                </td>
            </tr>
            <tr>
                <td align="center">
                	<table cellpadding="0" cellspacing="0" align="center" class="contenedorPrincipal">
                    	<tr>
                        	<td align="">
                        		<?php
                				switch($iOpcion)
                				{
                					case 1:
                					{
        						?>
        						<form name="frm" action="recepcionEquipos.php?iOpcion=2" method="post" onsubmit="return valida(1);">
        							<table width="400">
        						<?php
                						break;
                					}
                					case 2:
                					{
            					?>
            					<form name="frm" action="recepcionEquipos.php?iOpcion=3" method="post" onSubmit="return valida(2);">
            						<table>
            					<?php	
                						break;
                					}
                				}
        						?>
		                				<?php
		                				switch($iOpcion)
		                				{
		                					case 1:
		                					{
                						?>
		                				<tr>
		                					<td width="100">Numero de Remito de Ingreso :</td>
		                					<td><input type="text" name="txtNroRemito" size="20" maxlength="20" /></td><td><div id="txtHint"></div></td>
		                				</tr>
		                				<tr>
		                					<td colspan="2" align="center"><input type="submit" name="cmdEnviar" value="Continuar"></td>
		                				</tr>
                						<?php
		                						break;
		                					}
		                					case 2:
		                					{
            							?>
            							<tr>
            								<td colspan="3">Numero de Remitode Ingreso: <b><?php echo $_POST[txtNroRemito]; ?></b></td>
            							</tr>
            							<tr>
            								<td>&nbsp;</td>
            							</tr>
        								<tr>
        									<th class="listaHead">Marca</th>
        									<th class="listaHead">Modelo</th>
        									<th class="listaHead">Imei</th>
        								</tr>
        								<?php
        								$mySQL = "SELECT O.imei, M.nombreComercial, MC.nombre ";
										$mySQL = $mySQL . "FROM modelos M, ";
										$mySQL = $mySQL . "marcas MC, ";
										$mySQL = $mySQL . "ordenes O, ";
										$mySQL = $mySQL . "equipostransito ET ";
										$mySQL = $mySQL . "WHERE M.idModelo = O.idModelo ";
										$mySQL = $mySQL . "AND MC.idMarca = M.idMarca ";
										$mySQL = $mySQL . "AND ET.idOrden = O.idOrden ";    									
										$mySQL = $mySQL . "AND ET.nroRemito ='" . $_POST[txtNroRemito] . "'";
    									$result = readSQL($mySQL);
			    							
			    						$i = 0;			    							
		  				                foreach($result as $value)
		  				                {
		  				                	if($i%2==0)
		  				                		$class = "par";
		  				                	else
		  				                		$class = "impar";
				                		?>
				                		<tr>
				                			<td class="<?php echo $class; ?>"><?php echo $value['nombre'] ;?></td>
				                			<td class="<?php echo $class; ?>"><?php echo $value['nombreComercial'] ;?></td>
				                			<td class="<?php echo $class; ?>" align="right"><?php echo $value['imei'] ;?></td>
				                		</tr>
    									<?php
		  				                }
		  				                ?>
        								<tr>
        									<td colspan="3" align="center"><input type="submit" name="cmdEnviar" value="Recibir"></td>
        								</tr>
		                				<input type="hidden" name="txtRemitoDef" value="<?php echo $_POST[txtNroRemito]; ?>"/>
            							<?php
	                							break;
		                					}
		                				}
		                				?>
		                			</table>
        		            	</form>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
	</body>
</html>